<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编译GLib应用程序</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="GLib 参考手册">
<link rel="up" href="glib.html" title="GLib概述">
<link rel="prev" href="glib-cross-compiling.html" title="交叉编译GLib软件包">
<link rel="next" href="glib-running.html" title="运行GLib应用程序">
<meta name="generator" content="GTK-Doc V1.18 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="glib-cross-compiling.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="glib.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GLib 参考手册</th>
<td><a accesskey="n" href="glib-running.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="glib-compiling"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle">编译GLib应用程序</span></h2>
<p>编译GLib应用程序 — 如何编译GLib应用程序</p>
</td>
<td valign="top" align="right"></td>
</tr></table></div>
<div class="refsect1">
<a name="idp5529368"></a><h2>在UNIX上编译GLib应用程序</h2>
<p>为了编译GLib应用程序，你需要告诉编译器在哪里找到GLib的头文件及库. <span class="application">pkg-config</span> 工具会帮你.</p>
<p>下面的shell会话展示了 <span class="application">pkg-config</span> 如何工作 (输出可能因不同的系统有所不同): </p>
<pre class="programlisting">
$ pkg-config --cflags glib-2.0
 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include 
$ pkg-config --libs glib-2.0
 -L/usr/lib -lm -lglib-2.0  
</pre>
<p>
See the <a class="ulink" href="http://www.freedesktop.org/wiki/Software/pkg-config" target="_top">pkg-config website</a>
for more information about <span class="application">pkg-config</span>.
</p>
<p>
If your application uses or <span class="structname">GObject</span>
features, it must be compiled and linked with the options returned
by the following <span class="application">pkg-config</span> invocation:
</p>
<pre class="programlisting">
$ pkg-config --cflags --libs gobject-2.0
</pre>
<p>
</p>
<p>如果你的应用程序使用模块(modules), 则必须用以下的 <span class="application">pkg-config</span> 输出来编译及链接: </p>
<pre class="programlisting">
$ pkg-config --cflags --libs gmodule-no-export-2.0 
$ pkg-config --cflags --libs gmodule-2.0 
</pre>
<p> 两者的不同之处在于gmodule-2.0 为连接添加了<code class="option">--export-dynamic</code> 标志, 但它并不经常用到.</p>
<p>最简单的编译方式是使用shell的"反引号"特性. 如果你将命令放入反引号里 (<span class="emphasis"><em>不是单引号</em></span>), 那么它的输出会在命令行执行前 取代命令.所以编译一个GLib的Hello,World,你输入以下命令: </p>
<pre class="programlisting">
$ cc `pkg-config --cflags --libs glib-2.0` hello.c -o hello
</pre>
<p>
Deprecated GLib functions are annotated to make the compiler
emit warnings when they are used (e.g. with gcc, you need to use
the -Wdeprecated-declarations option). If these warnings are
problematic, they can be turned off by defining the preprocessor
symbol %GLIB_DISABLE_DEPRECATION_WARNINGS by using the commandline
option <code class="literal">-DGLIB_DISABLE_DEPRECATION_WARNINGS</code>
</p>
<p>
GLib deprecation annotations are versioned; by defining the
macros %GLIB_VERSION_MIN_REQUIRED and %GLIB_VERSION_MAX_ALLOWED,
you can specify the range of GLib versions whose API you want
to use. APIs that were deprecated before or introduced after
this range will trigger compiler warnings.
</p>
<p>
The older deprecation mechanism of hiding deprecated interfaces
entirely from the compiler by using the preprocessor symbol
G_DISABLE_DEPRECATED is still used for deprecated macros,
enumeration values, etc. To detect uses of these in your code,
use the commandline option <code class="literal">-DG_DISABLE_DEPRECATED</code>.
</p>
<p>
The recommended way of using GLib has always been to only include the
toplevel headers <code class="filename">glib.h</code>,
<code class="filename">glib-object.h</code>, <code class="filename">gio.h</code>.
Starting with 2.32, GLib enforces this by generating an error
when individual headers are directly included.
</p>
<p>
Still, there are some exceptions; these headers have to be included
separately:
<code class="filename">gmodule.h</code>,
<code class="filename">glib-unix.h</code>,
<code class="filename">glib/gi18n-lib.h</code> or
<code class="filename">glib/gi18n.h</code> (see
the <a class="link" href="glib-I18N.html" title="Internationalization">Internationalization section</a>),
<code class="filename">glib/gprintf.h</code> and
<code class="filename">glib/gstdio.h</code>
(we don't want to pull in all of stdio).
</p>
</div>
</div>
<div class="footer">
<hr>
          Generated by GTK-Doc V1.18</div>
</body>
</html>